﻿@page "/Bas/CodingList/Create"
@using nERP.ViewModel.Bas.CodingListVMs
@using nERP.Model.Bas
@using System.ComponentModel.DataAnnotations
@inherits BasePage

<div>
    <ValidateForm @ref="vform" Model="@Model" OnValidSubmit="@Submit">
        <Row ItemsPerRow="ItemsPerRow.Two" RowType="RowType.Normal">
            <BootstrapInput @bind-Value="@Model.Entity.Code" />
            <BootstrapInput @bind-Value="@Model.Entity.CurrentNumber" Readonly="true" />
            <Row ItemsPerRow="ItemsPerRow.One" ColSpan="2" RowType="RowType.Normal">
                <BootstrapInput @bind-Value="@Model.Entity.Example" Readonly="true" />
            </Row>
        </Row>
        <Divider Icon="fa-fw fa-solid fa-bookmark" Text="@WtmBlazor.Localizer["Sys.ExtraInfo"]" Alignment="Alignment.Left" />


        <Table TItem="CodingRule" @bind-Items="@CodingRuleCodingListList" OnDeleteAsync="@OnDeleteAsync" OnAddAsync="@OnAddAsync"
               ShowSkeleton="true" IsStriped="true" IsBordered="true" ShowRefresh="false" ShowToolbar="true" ShowExtendButtons="true" IsExcel="true">
            <TableColumns>
                <TableColumn @bind-Field="@context.Seq" Text="@WtmBlazor.Localizer["Page.序号"]" Readonly="true" Width="60" />
                <TableColumn @bind-Field="@context.CodeRule" Text="@WtmBlazor.Localizer["Page.规则"]" IsPopover="true" />
                <TableColumn @bind-Field="@context.FixedValue" Text="@WtmBlazor.Localizer["Page.固定值"]" />
                <TableColumn @bind-Field="@context.FixedRule" Text="@WtmBlazor.Localizer["Page.规则值"]" IsPopover="true" Width="200" />
                <TableColumn @bind-Field="@context.GlideLen" Text="@WtmBlazor.Localizer["Page.流水号长度"]" />
                <TableColumn @bind-Field="@context.StartNumber" Text="@WtmBlazor.Localizer["Page.流水号起始值"]" />
                <TableColumn @bind-Field="@context.GlideStep" Text="@WtmBlazor.Localizer["Page.递增步长"]" Readonly="true" />
                <TableColumn @bind-Field="@context.Glide" Text="@WtmBlazor.Localizer["Page.流水号补位码"]" Readonly="true" />
            </TableColumns>

        </Table>

        <div class="modal-footer table-modal-footer">
            <Button ButtonType="ButtonType.Submit" IsAsync="true" Text="@WtmBlazor.Localizer["Sys.Submit"]" />
            <Button OnClick="OnClose" Text="@WtmBlazor.Localizer["Sys.Close"]" />
        </div>

    </ValidateForm>

</div>



@code {
    [Parameter]
    public string id { get; set; }
    [Parameter]
    public string[] ids { get; set; }
    private CodingListVM Model = new CodingListVM();
    private ValidateForm vform { get; set; }
    private async Task Submit(EditContext context)
    {

        Model.Entity.CodingRule_CodingList = CodingRuleCodingListList.ToList();
        string SN = await GetNewSN(Model.Entity.CodingRule_CodingList);
        if (string.IsNullOrWhiteSpace(SN)) return;

        Model.Entity.Example = SN;
        await PostsForm(vform, "/api/Bas/CodingList/Create", (s) => "Sys.OprationSuccess");
    }
    public void OnClose()
    {
        CloseDialog();
    }

    private IEnumerable<CodingRule> CodingRuleCodingListList { get; set; } = new List<CodingRule>();
    private List<CodingRule> Items { get; set; } = new List<CodingRule>();
    protected override async Task OnInitializedAsync()
    {

        Model.Entity.CurrentNumber = 0;

        await base.OnInitializedAsync();
    }


    private Task<CodingRule> OnAddAsync()
    {
        var cr = new CodingRule()
            {
                ID = Guid.NewGuid(),
                Seq = (Items.Max(s => s.Seq) ?? 0) + 1,
                CodeRule = nERP.Model.CodeRuleEnum.Fixeds,
                StartNumber = 1,
                GlideStep = 1,
                Glide = "0"
            };
        if (Items.Count > 9)
        {
            WtmBlazor.Toast.Error(WtmBlazor.Localizer["Sys.Error"], $"不能超过10条规则");
            return Task.FromResult(cr);
        }
        //var i = Items.Max(s => s.Seq) ?? 0;

        Items.Insert(Items.Count, cr);
        CodingRuleCodingListList = Items;

        //var SN = GetNewSN2("入库单号");

        StateHasChanged();
        return Task.FromResult(cr);
    }
    private Task<bool> OnDeleteAsync(IEnumerable<CodingRule> items)
    {
        items.ToList().ForEach(i => Items.Remove(i));
        return Task.FromResult(true);
    }
}
